﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace PRE.catalog
{
    public partial class FrmDoanhNghiep : PRE.catalog.FrmBase
    {
        DAL.DoanhNghiepDAL _dal = new DAL.DoanhNghiepDAL();
        BLL.DoanhNghiepBLL _bll = new BLL.DoanhNghiepBLL();
        public FrmDoanhNghiep()
        {
            InitializeComponent();
        }
        
        #region Override

        /// <summary>
        /// Perform when click add button
        /// </summary>
        protected override void PerformAdd()
        {

            ReadOnlyControl(false);
            ClearDataBindings();
            ChangeStatus(false);

            string _ma = _bll.CreateID();
            txtmadn.Text = _ma;
            txttendn.ResetText();
            cbomalv.ItemIndex = -1;
            txtdiachi.ResetText();
            txtdienthoai.ResetText();
            txtfax.ResetText();
            
            txttendn.Focus();

            btxemchitiet.Enabled = false;
            base.PerformAdd();
        }

        /// <summary>
        /// Perform when click cancel button
        /// </summary>
        protected override void PerformCancel()
        {
            btxemchitiet.Enabled = true;
            base.PerformCancel();
        }
        /// <summary>
        /// Perform when click delete button
        /// </summary>
        /// <returns></returns>
        protected override void PerformDelete()
        {
            string id = txtmadn.Text.Trim();

            DialogResult result;
            var ok = _dal.Delete(id);
            result = MessageBox.Show("Bạn có chắc xóa không ? ", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (result == DialogResult.OK)
            {
                if (ok)
                {
                    PerformRefresh();
                }
                else
                    MessageBox.Show("Lỗi! Không xóa được", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            base.PerformDelete();
        }

        /// <summary>
        /// Perform when click edit button
        /// </summary>
        protected override void PerformEdit()
        {
            ReadOnlyControl(false);
            ChangeStatus(false);
            txttendn.Focus();
            btxemchitiet.Enabled = false;
            base.PerformEdit();
        }

        /// <summary>
        /// Perform when click print button
        /// </summary>
        protected override void PerformPrint()
        {
            base.PerformPrint();
        }

        /// <summary>
        /// Load data or perform when click refresh button
        /// </summary>
        protected override void PerformRefresh()
        {
            ReadOnlyControl();
            LoadData();

            if (_dtb != null)
            {
                // Binding data
                ClearDataBindings();
                if (_dtb.Rows.Count > 0) DataBindingControl();
            }
            
            base.PerformRefresh();
        }

        /// <summary>
        /// Perform when click save button
        /// </summary>
        protected override void PerformSave()
        {
            var name = Proper(txttendn.Text.Trim());



            if (IsAdd)
            {
                var o = new DAL.Entities.doanhnghiep()
                {
                    madn = txtmadn.Text.Trim(),
                    tendn = name,
                    malv=(cbomalv.EditValue+"").Trim(),
                    diachi=txtdiachi.Text.Trim(),
                    dienthoai=txtdienthoai.Text.Trim(),
                    fax=txtfax.Text.Trim()

                };

                var oki = _dal.Insert(o);
                if (oki)
                {
                    MessageBox.Show("Đã lưu thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    PerformRefresh();
                    ChangeStatus(false);
                    btxemchitiet.Enabled = false;
                    PerformAdd();
                }
                else
                {
                    MessageBox.Show("Lỗi! Lưu thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    ChangeStatus(false);
                    ReadOnlyControl(false);
                }

            }

            if (IsEdit)
            {

                var o = new DAL.Entities.doanhnghiep()
                {
                    madn = txtmadn.Text.Trim(),
                    tendn = name,
                    malv = (cbomalv.EditValue + "").Trim(),
                    diachi = txtdiachi.Text.Trim(),
                    dienthoai = txtdienthoai.Text.Trim(),
                    fax = txtfax.Text.Trim()

                };

                var oki = _dal.Update(o);
                if (oki)
                {
                    MessageBox.Show("Đã lưu thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    PerformRefresh();
                    btxemchitiet.Enabled = true ;
                    ChangeStatus();
                }
                else
                {
                    MessageBox.Show("Lỗi! Lưu thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    ChangeStatus(false);
                    ReadOnlyControl(false);
                }
            }


            base.PerformSave();
        }

        /// <summary>
        /// Reset all input control
        /// </summary>
        protected override void ResetText()
        {

            cbomalv.ItemIndex = -1;
            txttendn.Text = null;
            txtmadn.Text = null;
            txtfax.Text = null;
            txtdienthoai.Text = null;
            txtdiachi.Text = null;
            
            base.ResetText();
        }

        /// <summary>
        /// Clear data binding
        /// </summary>
        protected override void ClearDataBindings()
        {
            cbomalv.DataBindings.Clear();
            txttendn.DataBindings.Clear();
            txtmadn.DataBindings.Clear();
            txtdiachi.DataBindings.Clear();
            txtdienthoai.DataBindings.Clear();
            txtfax.DataBindings.Clear();



            base.ClearDataBindings();
        }

        /// <summary>
        /// Add data binding
        /// </summary>
        protected override void DataBindingControl()
        {
            cbomalv.DataBindings.Add("EditValue", _dtb, ".MSLV");
            txttendn.DataBindings.Add("EditValue", _dtb, ".TENDN");
            txtmadn.DataBindings.Add("EditValue", _dtb, ".MSDN");
            txtdiachi.DataBindings.Add("EditValue", _dtb, ".DC");
            txtdienthoai.DataBindings.Add("EditValue", _dtb, ".DT");
            txtfax.DataBindings.Add("EditValue", _dtb, ".FAX");



            base.DataBindingControl();
        }

        /// <summary>
        /// Set read only control on form
        /// </summary>
        /// <param name="isReadOnly">Read only is trule else normal</param>
        protected override void ReadOnlyControl(bool isReadOnly = true)
        {
           cbomalv.Properties.ReadOnly = isReadOnly;
            txttendn.Properties.ReadOnly = isReadOnly;
            txtmadn.Properties.ReadOnly = true;
            txtdiachi.Properties.ReadOnly = isReadOnly;
            txtdienthoai.Properties.ReadOnly = isReadOnly;
            txtfax.Properties.ReadOnly = isReadOnly;
            grcMain.Enabled = isReadOnly;

            base.ReadOnlyControl(isReadOnly);
        }

        protected override void PerformSearch()
        {
            base.PerformSearch();
        }
      
        /// <summary>
        /// Update object
        /// </summary>
        /// <returns>True if successful else false</returns>
        protected override bool UpdateObject()
        {
            return base.UpdateObject();
        }

        /// <summary>
        /// Insert object
        /// </summary>
        /// <returns>True if successful else false</returns>
        protected override bool InsertObject()
        {
            return base.InsertObject();
        }

        /// <summary>
        /// Load data
        /// </summary>
        protected override void LoadData()
        {

            _dtb = _dal.Select();


            if (_dtb != null)
            {
                grcMain.DataSource = _dtb;

            }

            base.LoadData();
        }
        #endregion

        private void FrmDoanhNghiep_Load(object sender, EventArgs e)
        {

            cbomalv.Properties.DataSource = _dal.select_linhvuc();
            PerformRefresh();
        }

        private void btxemchitiet_Click(object sender, EventArgs e)
        {
            _madn = txtmadn.Text.Trim();
            _tendn = txttendn.Text.Trim();
            _dcdn = txtdiachi.Text.Trim();
            _sdt = txtdienthoai.Text.Trim();
            _fax = txtfax.Text.Trim();
            _malv = (cbomalv.EditValue + "").Trim();
            var frm = new Thongke_Baocao.FrmThongKeCacSanPhamCuaDoanhNghiep();
            frm._msDN = _madn;
            frm._tenDN = _tendn;
            frm._diachi = _dcdn;
            frm._dienthoai = _sdt;
            frm._fax = _fax;
            frm._malv = _malv;
            frm.ShowDialog();
        }
        public string _madn;
        public string _tendn;
        public string _dcdn;
        public string _sdt;
        public string _fax;
        public string _malv;    
    }
}
